// sm4g.c
#include <stdio.h>
#include <string.h>
#include <gmssl/sm4.h>

int main() {
    // 16 字节密钥
    uint8_t key[16] = {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
    };

    // 明文必须是 16 字节（ECB 模式要求）
    uint8_t plaintext[16] = "231318fushuling"; // 15 字符 + 1 个 '!' = 16 字节
    uint8_t ciphertext[16];
    uint8_t decrypted[16];

    SM4_KEY sm4_key;

    // === 加密 ===
    sm4_set_encrypt_key(&sm4_key, key);
    sm4_encrypt(&sm4_key, plaintext, ciphertext);

    printf("Ciphertext: ");
    for (int i = 0; i < 16; i++) {
        printf("%02x", ciphertext[i]);
    }
    printf("\n");

    // === 解密 ===
    sm4_set_decrypt_key(&sm4_key, key);   // 设置为解密密钥
    sm4_encrypt(&sm4_key, ciphertext, decrypted);  // 仍然调用 sm4_encrypt！

    decrypted[15] = '\0'; // 确保字符串结尾
    printf("Decrypted: %s\n", decrypted);

    return 0;
}
